<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['environment:shouxu:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['environment:shouxu:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['environment:shouxu:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['environment:shouxu:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="shouxuList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="环评手续ID" align="center" prop="esId" />-->
      <el-table-column :formatter="typeFormat_p" label="建设项目" align="center" prop="esProjectId" />
      <el-table-column :formatter="typeFormat" label="环评报告" align="center" prop="esBaogao" />
      <el-table-column label="环评编制单位名称" align="center" prop="esBzName" />
<!--      <el-table-column label="环评批复文号" align="center" prop="esPifu" />-->
      <el-table-column label="环评审批单位" align="center" prop="esShenpi" />
      <el-table-column label="环评审批时间" align="center" prop="esShenpiTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.esShenpiTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="环评批复文件" align="center" prop="esPifuFile" />
      <el-table-column label="竣工环境保护验收" align="center" prop="esYanshouFile" />
<!--      <el-table-column label="试运行申请" align="center" prop="esShiFile" />
      <el-table-column label="试运行批复" align="center" prop="esShiPfFile" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="small"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['environment:shouxu:edit']"
          >修改</el-button>
          <el-button
            size="small"
            @click="handleDelete(scope.row)"
            v-hasPermi="['environment:shouxu:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改环评报告，批复及竣工验收对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="30%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="建设项目" prop="esProjectId">
          <el-select v-model="form.esProjectId" placeholder="请选择">
            <el-option
              v-for="dict in projectList"
              :key="dict.pcId"
              :label="dict.pcName"
              :value="dict.pcId"
            ></el-option>
          </el-select>
<!--          <el-input v-model="form.esProjectId" placeholder="请输入建设项目" />-->
        </el-form-item>
        <el-form-item label="环评报告" prop="esBaogao">
          <el-select v-model="form.esBaogao" placeholder="请选择">
            <el-option
              v-for="dict in typeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="环评编制单位名称" prop="esBzName">
          <el-input v-model="form.esBzName" placeholder="请输入环评编制单位名称" />
        </el-form-item>
        <el-form-item label="环评批复文号" prop="esPifu">
          <el-input v-model="form.esPifu" placeholder="请输入环评批复文号" />
        </el-form-item>
        <el-form-item label="环评审批单位" prop="esShenpi">
          <el-input v-model="form.esShenpi" placeholder="请输入环评审批单位" />
        </el-form-item>
        <el-form-item label="环评审批时间" prop="esShenpiTime">
          <el-date-picker clearable size="small"
            v-model="form.esShenpiTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择环评审批时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="环评批复文件">
          <fileUpload v-model="form.esPifuFile"/>
        </el-form-item>
        <el-form-item label="竣工环境保护验收">
          <fileUpload v-model="form.esYanshouFile"/>
        </el-form-item>
        <el-form-item label="试运行申请">
          <fileUpload v-model="form.esShiFile"/>
        </el-form-item>
        <el-form-item label="试运行批复">
          <fileUpload v-model="form.esShiPfFile"/>
        </el-form-item>
<!--        <el-form-item label="企业ID" prop="epId">
          <el-input v-model="form.epId" placeholder="请输入企业ID" />
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listShouxu, getShouxu, delShouxu, addShouxu, updateShouxu, exportShouxu } from "@/api/environment/shouxu";
import {listConstruction} from "@/api/enterprise/construction";
import {InfoItem} from "@/api/enterprise/info";
export default {
  name: "Shouxu",
  data() {
    return {
      vv:137,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 环评报告，批复及竣工验收表格数据
      shouxuList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        epId: [
          { required: true, message: "企业ID不能为空", trigger: "blur" }
        ],
      },
      typeOptions:[],
      typeOptions_p:[],
      projectList:{},
    };
  },
  created() {
    this.getProjectList();
    this.getList();
    this.getDicts("hp").then(response => {
      this.typeOptions = response.data;
    });
  },
  methods: {
    selectLabel(datas, value) {
      var actions = [];
      Object.keys(datas).some((key) => {
        if (datas[key].pcId == ('' + value)) {
          actions.push(datas[key].pcName);
          return true;
        }
      })
      return actions.join('');
    },
    typeFormat(row, column) {
      //console.log(this.typeOptions);
      return this.selectDictLabel(this.typeOptions, row.esBaogao);
    },
    typeFormat_p(row, column) {
      return this.selectLabel(this.typeOptions_p, row.esProjectId);
    },
    getProjectList(){
      listConstruction().then(res=>{
        this.projectList = res.rows;
        this.typeOptions_p = res.rows;
      })
    },
    /** 查询环评报告，批复及竣工验收列表 */
    getList() {
      this.loading = true;
      const username = this.$store.state.user.name;
      InfoItem(username).then(res => {
        if (res.hasOwnProperty("data")) {
          this.queryParams.epId = res.data.epId;
          listShouxu(this.queryParams).then(response => {
            this.shouxuList = response.rows;
            this.total = response.total;
            this.loading = false;
          });
        }
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        esId: null,
        esProjectId: null,
        esBaogao: null,
        esBzName: null,
        esPifu: null,
        esShenpi: null,
        esShenpiTime: null,
        esPifuFile: null,
        esYanshouFile: null,
        esShiFile: null,
        esShiPfFile: null,
        epId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        other: null,
        otherone: null,
        othertwo: null,
        otherthree: null,
        otherfour: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.esId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "环评报告，批复及竣工验收";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const esId = row.esId || this.ids
      getShouxu(esId).then(response => {
        this.form = response.data
        this.form.esProjectId= parseInt(this.form.esProjectId)
        this.open = true;
        this.title = "修改环评报告，批复及竣工验收";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.esId != null) {
            updateShouxu(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            const username = this.$store.state.user.name;
            InfoItem(username).then(res => {
              if (res.hasOwnProperty("data")) {
                this.form.epId = res.data.epId;
              }
              addShouxu(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const esIds = row.esId || this.ids;
      this.$confirm('是否确认删除环评报告，批复及竣工验收编号为"' + esIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delShouxu(esIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有环评报告，批复及竣工验收数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportShouxu(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
